package com.zd.utils;

import com.zd.domain.Course;
import com.zd.domain.Order;
import com.zd.service.course.CourseService;
import com.zd.service.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * 随机生成7位数的课程号
 */
@Component
public class CourseIdGeneration {

    private static CourseService courseService;

    private static OrderService orderService;

    // 你可能需要一个构造函数或者其他方法来初始化courseService和orderService
    @Autowired
    public CourseIdGeneration(CourseService courseService, OrderService orderService) {
        this.courseService = courseService;
        this.orderService = orderService;
    }

    /**
     * 随机生成7位数（课程id）
     * @return
     */
    public static int generateRandom7DigitNumber() {
        while (true) {
            Random rand = new Random();
            int goodId = rand.nextInt(9000000) + 1000000; // 生成7位数

            Course course = courseService.findCourseById(goodId);
            if (course == null){
                return goodId;
            }
        }
    }

    /**
     * 随机生成9位数（订单编号）
     * @return
     */
    public static int generateRandom9DigitNumber() {
        while (true) {
            Random rand = new Random();
            int orderNo = rand.nextInt(900000000) + 100000000;  // 生成9位数

            Order order = orderService.findOrderByOrderNo(orderNo);

            if (order == null){
                return orderNo;
            }
        }
    }
}
